package loan.calc.util;



import org.springframework.util.StopWatch;

import java.text.NumberFormat;

/**
 *
 * @author v_ECD963
 */
public class EnhanceStopWatch extends StopWatch {

    /**
     * Construct a new {@code StopWatch}.
     * <p>Does not start any task.
     */
    public EnhanceStopWatch() {
        super();
    }

    /**
     * Construct a new {@code StopWatch} with the given ID.
     * <p>The ID is handy when we have output from multiple stop watches and need
     * to distinguish between them.
     * <p>Does not start any task.
     *
     * @param id identifier for this stop watch
     */
    public EnhanceStopWatch(String id) {
        super(id);
    }

    /**
     * 毫秒形式输出
     * @return
     */
    public String prettyPrintWithMillis() {
        StringBuilder sb = new StringBuilder(shortSummaryMillis());
        sb.append('\n');

        sb.append("-----------------------------------------\n");
        sb.append("ms     %     Task name\n");
        sb.append("-----------------------------------------\n");
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMinimumIntegerDigits(5);
        nf.setGroupingUsed(false);
        NumberFormat pf = NumberFormat.getPercentInstance();
        pf.setMinimumIntegerDigits(3);
        pf.setGroupingUsed(false);
        for (TaskInfo task : getTaskInfo()) {
            sb.append(nf.format(task.getTimeMillis())).append("  ");
            sb.append(pf.format(task.getTimeSeconds() / getTotalTimeSeconds())).append("  ");
            sb.append(task.getTaskName()).append("\n");
        }
        return sb.toString();
    }

    public String shortSummaryMillis() {
        return "StopWatch '" + getId() + "': running time = " + getTotalTimeMillis() + " ms";
    }
}
